package domain;

import java.util.ArrayList;
import java.util.Random;

public class Board {
	
	private Card[][] arr;
	private Card card;
	private ArrayList<Card> itemList;
	int a;
	
	public void initBoard(int rowCount,int colCount){
		Random rnd = new Random();
		a=rowCount*colCount/2;
		itemList= new ArrayList<Card>(a);
		arr = new Card[rowCount][colCount];
		
		for(int b=0;b<a;b++){
			card=new Card((int) (rnd.nextInt(a)));
			itemList.add(card);
			//for pair
			itemList.add(card);
		}
		shuffleArray(itemList);
		int i=0;
		for(int row=0; row<rowCount; row++){
			for (int col = 0; col < colCount; col++){		
				arr[row][col] = itemList.get(i);
				i++;
			}
		}

	}
	
	public Card getBoard(int r,int c){
		return this.arr[r][c] ;
	}

	private void shuffleArray(ArrayList<Card> itemList2) {
		int n = itemList2.size();
		Random random = new Random();
		random.nextInt();
		for (int i = 0; i < n; i++) {
			int change = i + random.nextInt(n - i);
			swap(itemList2, i, change);
		}
	}

	private static void swap(ArrayList<Card> a, int i, int change) {
		Card helper = a.get(i);
		a.set(i, a.get(change));
		a.set(change, helper);

	}
	
}
